// ==UserScript==
// @name         定时获取脚本的反馈
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @description  脚本猫定时获取`视频自动网页全屏`脚本的反馈
// @author       Feny
// @crontab      * once * * *
// @connect      greasyfork.org
// @grant        GM_notification
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_openInTab
// @grant        GM.xmlHttpRequest
// @icon         
// ==/UserScript==

return new Promise(async (resolve) => {
  const cacheKey = "feedback-number";
  const url = "https://greasyfork.org/zh-CN/scripts/519872-视频自动网页全屏-倍速播放";

  function parseHtml(htmlStr) {
    const parser = new DOMParser();
    const doc = parser.parseFromString(htmlStr, "text/html");
    return doc.body;
  }

  function extractNumber(text) {
    const match = text.match(/\d+/);
    return match ? parseInt(match[0], 10) : 0;
  }

  try {
    const res = await GM.xmlHttpRequest({ url, timeout: 5000 });
    const docBody = parseHtml(res.responseText);

    const feedbackElement = docBody.querySelector("#script-links li:nth-child(4)");
    const currentNum = extractNumber(feedbackElement.innerText);
    if (GM_getValue(cacheKey, 0) === currentNum) return resolve();

    GM_notification({
      url,
      timeout: 3000,
      title: "定时脚本通知",
      text: "视频自动网页全屏｜倍速播放脚本 有新的反馈",
      onclick: () => {
        GM_openInTab(url);
        GM_setValue(cacheKey, currentNum);
      },
    });
  } catch (e) {}

  resolve();
});
